package cn.gson.springboot.model.service.afterservice;

import cn.gson.springboot.model.dao.afterdao.CulogDao;
import cn.gson.springboot.model.dao.afterdao.ReplyDao;
import cn.gson.springboot.model.mapper.aftermapper.CulogMapper;
import cn.gson.springboot.model.mapper.aftermapper.ReplyMapper;
import cn.gson.springboot.model.pjos.Culog;
import cn.gson.springboot.model.pjos.Emp;
import cn.gson.springboot.model.pjos.Reply;

import cn.gson.springboot.model.vo.aftervo.ReplyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ReplyService {

    @Autowired
    ReplyDao dao;
    @Autowired
    CulogDao logDao;
    @Autowired
    ReplyMapper mapper;

    //回复表查询
    public List<Reply> selectByCulId(Integer culId){
        return mapper.replByCulId(culId);
    }

    //二级回复查询
    public List<Reply> selectByCulIdTwo(Integer culId){
        return mapper.replByCulIdTwo(culId);
    }

    //新增回复表
    public void addReply(Integer empId,String content,Integer culId){
        Timestamp time = new Timestamp(new Date().getTime());
        Reply repl = new Reply();
        repl.setReplDate(time);
        repl.setReplContent(content);
        repl.setReplDelete(1);
        Culog log = new Culog();
        if(culId != null){
            log.setCulId(culId);
            repl.setCulogByCulId(log);
        }
        Emp emp = new Emp();
        emp.setEmployeesId(empId);
        repl.setEmpByEmployeesId(emp);
        dao.save(repl);
        Culog logs = logDao.findById(culId).get();
        logs.setCulStatus("已点评");
        logDao.save(logs);
    }

    //逻辑删除
    public void deleteReplId(Integer replId){
        Reply repl = dao.findById(replId).get();
        repl.setReplDelete(0);
        dao.save(repl);
    }

    //新增二级回复
    public void addReplys(ReplyVo replvo){
        Timestamp time = new Timestamp(new Date().getTime());
        Reply repl = new Reply();
        repl.setReplDate(time);
        repl.setReplIds(replvo.getReplIds());
        repl.setReplContent(replvo.getContents());
        repl.setReplDelete(1);
        repl.setReplPeople(replvo.getEmployeesId());
        Culog log = new Culog();
        if(replvo.getCulId() != null){
            log.setCulId(replvo.getCulId());
            repl.setCulogByCulId(log);
        }
        Emp emp = new Emp();
        emp.setEmployeesId(replvo.getEmpId());
        repl.setEmpByEmployeesId(emp);
        dao.save(repl);
    }
}
